package com.example.h1909aapp.activity;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import com.example.h1909aapp.R;
import com.example.h1909aapp.util.LogUtil;
import com.example.h1909aapp.util.ToastUtil;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;

import java.util.Map;

public class LoginActivity_1 extends AppCompatActivity implements View.OnClickListener {

    private Button mBtnShare;
    private Button mBtnShareQq;
    private Button mBtnShareWechat;
    private Button mBtnShareSina;
    private Button mBtnLoginQq;
    private Button mBtnLoginWechat;
    private Button mBtnLoginSina;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ActivityCompat.requestPermissions(this, new String[]{
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE
        }, 1);
        initView();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }

    private void initView() {
        mBtnShare = (Button) findViewById(R.id.btn_share);
        mBtnShare.setOnClickListener(this);
        mBtnShareQq = (Button) findViewById(R.id.btn_share_qq);
        mBtnShareQq.setOnClickListener(this);
        mBtnShareWechat = (Button) findViewById(R.id.btn_share_wechat);
        mBtnShareWechat.setOnClickListener(this);
        mBtnShareSina = (Button) findViewById(R.id.btn_share_sina);
        mBtnShareSina.setOnClickListener(this);
        mBtnLoginQq = (Button) findViewById(R.id.btn_login_qq);
        mBtnLoginQq.setOnClickListener(this);
        mBtnLoginWechat = (Button) findViewById(R.id.btn_login_wechat);
        mBtnLoginWechat.setOnClickListener(this);
        mBtnLoginSina = (Button) findViewById(R.id.btn_login_sina);
        mBtnLoginSina.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_share:
                initShare();
                break;
            case R.id.btn_share_qq:
                share(SHARE_MEDIA.QQ);
                break;
            case R.id.btn_share_wechat:
                share(SHARE_MEDIA.WEIXIN);
                break;
            case R.id.btn_share_sina:
                share(SHARE_MEDIA.SINA);
                break;
            case R.id.btn_login_qq:
                login(SHARE_MEDIA.QQ);
                break;
            case R.id.btn_login_wechat:
                login(SHARE_MEDIA.WEIXIN);
                break;
            case R.id.btn_login_sina:
                login(SHARE_MEDIA.SINA);
                break;
        }
    }

    private void login(SHARE_MEDIA type) {
        UMShareAPI.get(this).getPlatformInfo(this, type, authListener);
    }
    UMAuthListener authListener = new UMAuthListener() {
        /**
         * @desc 授权开始的回调
         * @param platform 平台名称
         */
        @Override
        public void onStart(SHARE_MEDIA platform) {

        }

        /**
         * @desc 授权成功的回调
         * @param platform 平台名称
         * @param action 行为序号，开发者用不上
         * @param data 用户资料返回
         */
        @Override
        public void onComplete(SHARE_MEDIA platform, int action, Map<String, String> data) {

            ToastUtil.showToastLong("成功");
            printData(data);
        }

        /**
         * @desc 授权失败的回调
         * @param platform 平台名称
         * @param action 行为序号，开发者用不上
         * @param t 错误原因
         */
        @Override
        public void onError(SHARE_MEDIA platform, int action, Throwable t) {
            ToastUtil.showToastLong("失败");
        }

        /**
         * @desc 授权取消的回调
         * @param platform 平台名称
         * @param action 行为序号，开发者用不上
         */
        @Override
        public void onCancel(SHARE_MEDIA platform, int action) {
            ToastUtil.showToastLong("取消");
        }
    };

    private void printData(Map<String, String> data) {
        for (Map.Entry<String,String> entry : data.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();
            LogUtil.print(key + ":" + value);
        }
    }

    private void share(SHARE_MEDIA type) {
        UMImage image = new UMImage(this, R.drawable.umeng_socialize_copyurl);//资源文件
        new ShareAction(this)
                .setPlatform(type)//传入平台
                .withMedia(image)
                .withText("hello")//分享内容
                .setCallback(shareListener)//回调监听器
                .share();
    }

    private void initShare() {
        UMImage image = new UMImage(this, R.drawable.umeng_socialize_fav);//资源文件
        new ShareAction(this)
                .withText("hello")
                .withMedia(image)
                .setDisplayList(SHARE_MEDIA.SINA, SHARE_MEDIA.QQ, SHARE_MEDIA.WEIXIN)
                .setCallback(shareListener).open();
    }

    private UMShareListener shareListener = new UMShareListener() {
        /**
         * @descrption 分享开始的回调
         * @param platform 平台类型
         */
        @Override
        public void onStart(SHARE_MEDIA platform) {

        }

        /**
         * @descrption 分享成功的回调
         * @param platform 平台类型
         */
        @Override
        public void onResult(SHARE_MEDIA platform) {
            ToastUtil.showToastLong("成功");
        }

        /**
         * @descrption 分享失败的回调
         * @param platform 平台类型
         * @param t 错误原因
         */
        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            ToastUtil.showToastLong("失败");
        }

        /**
         * @descrption 分享取消的回调
         * @param platform 平台类型
         */
        @Override
        public void onCancel(SHARE_MEDIA platform) {
            ToastUtil.showToastLong("取消");
        }
    };
}
